from rest_framework import serializers

from jqgame.basegame import GameType
from jqgame.utils import get_game_name


class UserPackageGoodsSerializer(serializers.Serializer):
    uuid = serializers.UUIDField(read_only=True)
    game_type = serializers.StringRelatedField(read_only=True)
    game_type_name = serializers.SerializerMethodField()
    good_uuid = serializers.UUIDField(read_only=True)
    good_name = serializers.StringRelatedField(read_only=True)
    good_display_url = serializers.StringRelatedField(read_only=True)
    good_price = serializers.IntegerField(read_only=True)
    good_recovery_price = serializers.IntegerField(read_only=True)
    good_level_title = serializers.StringRelatedField(read_only=True)
    count = serializers.SerializerMethodField(read_only=True)
    createdAt = serializers.DateTimeField("%Y-%m-%d %H:%M:%S", read_only=True)

    def get_game_type_name(self, obj):
        game_type = obj.get("game_type")
        if game_type is None:
            return None
        return get_game_name(GameType(game_type))

    def get_count(self, obj):
        return 1


class UserPackageGoodsV2Serializer(serializers.Serializer):
    uuid = serializers.UUIDField(read_only=True)
    game_type = serializers.StringRelatedField(read_only=True)
    game_type_name = serializers.SerializerMethodField()
    good_uuid = serializers.UUIDField(read_only=True)
    good_name = serializers.StringRelatedField(read_only=True)
    good_display_url = serializers.StringRelatedField(read_only=True)
    good_price = serializers.IntegerField(read_only=True)
    good_recovery_price = serializers.IntegerField(read_only=True)
    good_level_title = serializers.StringRelatedField(read_only=True)
    createdAt = serializers.DateTimeField("%Y-%m-%d %H:%M:%S", read_only=True)
    sum_good_price = serializers.IntegerField(read_only=True)
    sum_good_recovery_price = serializers.IntegerField(read_only=True)
    sum_good_count = serializers.IntegerField(read_only=True)

    def get_game_type_name(self, obj):
        game_type = obj.get("game_type")
        if game_type is None:
            return None
        return get_game_name(GameType(game_type))
